package org.zpmis.api.http;

import org.apache.ibatis.annotations.Delete;
import org.zpmis.api.business.SysUserWebService;
import org.zpmis.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.domain.user.qo.AddSysUserQo;
import org.zpmis.domain.user.qo.EditSysUserQo;
import org.zpmis.domain.user.qo.ResetUserPwdQo;
import org.zpmis.domain.user.qo.SysUserListQo;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
* created with CodeGenerator
* description:
* @author  Yang
* date: 2021-5-31
* time: 13:55:42
*/
@RestController
@RequestMapping("/sys/user")
@Slf4j
public class SysUserController extends BaseController{

    @Resource
    private SysUserWebService sysUserWebService;


    /**
     * 获取系统所有的内部用户 下拉使用
     * @return
     */
    @PostMapping("/allSysUser")
    public PlatformResult allSysUser(){
        try {
            return PlatformResult.success(sysUserWebService.allSysUser());
        } catch (BusinessException e) {
            log.error("[SysUserController]---allSysUser--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysUserController]---allSysUser--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 获取系统所有内部用户
     * 20210908 gf
     * @param showEmployee 显示雇员姓名 1-显示雇员姓名 0(或其他数字)-显示登录账户即登录名
     * @return
     */
    @PostMapping("/allSysUser/{showEmployee}")
    public PlatformResult allSysUserShowEmployee(@PathVariable String showEmployee){
        try {
            if(StringUtil.equals(showEmployee,"1")){
                return PlatformResult.success(sysUserWebService.allSysUserShowEmployee());
            }else {
                return PlatformResult.success(sysUserWebService.allSysUser());
            }
        } catch (BusinessException e) {
            log.error("[SysUserController]---allSysUser--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysUserController]---allSysUser--->Exception:",e);
            return PlatformResult.failure();
        }
    }
    /**
     * 新增用户
     * @param addSysUserQo
     * @return
     */
    @PostMapping("/addSysUser")
    public PlatformResult addSysUser(@Valid @RequestBody AddSysUserQo addSysUserQo){
        try {
            sysUserWebService.addSysUser(addSysUserQo);
        } catch (BusinessException e) {
            log.error("[SysUserController]---addSysUser--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysUserController]---addSysUser--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 修改用户信息
     * @param editSysUserQo
     * @return
     */
    @PutMapping("editSysUser")
    public PlatformResult editSysUser(@Valid @RequestBody EditSysUserQo editSysUserQo){
        try {
            sysUserWebService.editSysUser(editSysUserQo);
        }catch (BusinessException e) {
            log.error("[SysUserController]---editSysUser--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysUserController]---editSysUser--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }


    @PostMapping("sysUserList")
    public PlatformResult sysUserList(@Valid @RequestBody SysUserListQo sysUserListQo){
        try {
            return PlatformResult.success(sysUserWebService.sysUserList(sysUserListQo));
        }catch (BusinessException e) {
            log.error("[SysUserController]---sysUserList--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        }catch (Exception e){
            log.error("[SysUserController]---sysUserList--->Exception:",e);
            return PlatformResult.failure();
        }
    }
    /**
     * 重置用户密码
     * @return
     */
    @PutMapping("resetUserPwd")
    public PlatformResult resetUserPwd(@Valid @RequestBody ResetUserPwdQo resetUserPwdQo){
        try {
            sysUserWebService.resetUserPwd(resetUserPwdQo);
        }catch (BusinessException e) {
            log.error("[SysUserController]---resetUserPwd--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysUserController]---resetUserPwd--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("deleteSysUser/{id}")
    public PlatformResult deleteSysUser(@PathVariable("id") Long id){
        try {
            sysUserWebService.deleteSysUser(id);
        }catch (BusinessException e) {
            log.error("[SysUserController]---deleteSysUser--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysUserController]---deleteSysUser--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }
}
